<template>
  <div class="page-container">
    <button class="back-button" @click="goToHome">
      <span class="icon-arrow-left"></span>
      返回首页
    </button>
    <h1>资源分配</h1>
    <p>管理工单所需资源的分配与调度</p>
    
    <div class="dispatch-card">
      <div class="filter-bar">
        <select v-model="selectedWorkOrder" class="form-control">
          <option value="">选择工单</option>
          <option v-for="order in workOrders" :key="order.id" :value="order.id">
            {{ order.number }} - {{ order.title }}
          </option>
        </select>
        
        <select v-model="resourceType" class="form-control">
          <option value="">资源类型</option>
          <option value="equipment">设备</option>
          <option value="material">材料</option>
          <option value="tool">工具</option>
        </select>
        
        <button class="btn-search" @click="searchResources">查询</button>
      </div>
      
      <div class="resource-list">
        <table class="data-table">
          <thead>
            <tr>
              <th>资源名称</th>
              <th>类型</th>
              <th>当前状态</th>
              <th>库存数量</th>
              <th>已分配</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="resource in filteredResources" :key="resource.id">
              <td>{{ resource.name }}</td>
              <td>{{ resource.typeLabel }}</td>
              <td><span :class="['status-tag', resource.status]">{{ resource.statusLabel }}</span></td>
              <td>{{ resource.stock }}</td>
              <td>{{ resource.assigned }}</td>
              <td>
                <button class="btn-assign" @click="assignResource(resource)" 
                  :disabled="resource.status !== 'available'">
                  分配
                </button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    
    <!-- 分配弹窗 -->
    <div v-if="showAssignModal" class="modal-overlay">
      <div class="modal">
        <div class="modal-header">
          <h3>分配资源: {{ currentResource?.name }}</h3>
          <button class="close-btn" @click="showAssignModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>分配数量:</label>
            <input type="number" v-model="assignQuantity" min="1" 
              :max="currentResource?.stock - currentResource?.assigned" class="form-control">
          </div>
          <div class="form-group">
            <label>分配给:</label>
            <select v-model="assignTo" class="form-control">
              <option value="">选择团队/人员</option>
              <option v-for="member in teamMembers" :key="member.id" :value="member.id">
                {{ member.name }}
              </option>
            </select>
          </div>
          <div class="form-group">
            <label>备注:</label>
            <textarea v-model="assignRemark" class="form-control" rows="3"></textarea>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn-cancel" @click="showAssignModal = false">取消</button>
          <button class="btn-confirm" @click="confirmAssignment">确认分配</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ResourceDispatch',
  data() {
    return {
      selectedWorkOrder: '',
      resourceType: '',
      workOrders: [
        { id: 1, number: 'WO-2023-001', title: '办公室网络升级' },
        { id: 2, number: 'WO-2023-002', title: '生产车间设备维护' },
        { id: 3, number: 'WO-2023-003', title: '仓库整理与盘点' }
      ],
      resources: [
        { id: 1, name: '笔记本电脑', type: 'equipment', typeLabel: '设备', status: 'available', statusLabel: '可用', stock: 10, assigned: 3 },
        { id: 2, name: '网线', type: 'material', typeLabel: '材料', status: 'available', statusLabel: '可用', stock: 100, assigned: 45 },
        { id: 3, name: '螺丝刀套装', type: 'tool', typeLabel: '工具', status: 'maintenance', statusLabel: '维护中', stock: 8, assigned: 5 },
        { id: 4, name: '打印机', type: 'equipment', typeLabel: '设备', status: 'available', statusLabel: '可用', stock: 3, assigned: 1 },
        { id: 5, name: '标签纸', type: 'material', typeLabel: '材料', status: 'low', statusLabel: '库存低', stock: 15, assigned: 10 }
      ],
      teamMembers: [
        { id: 1, name: '张三' },
        { id: 2, name: '李四' },
        { id: 3, name: '王五' }
      ],
      showAssignModal: false,
      currentResource: null,
      assignQuantity: 1,
      assignTo: '',
      assignRemark: ''
    }
  },
  computed: {
    filteredResources() {
      return this.resources.filter(resource => {
        if (this.resourceType && resource.type !== this.resourceType) return false;
        return true;
      });
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    searchResources() {
      // 实际项目中这里会调用API获取数据
      console.log('搜索资源', this.selectedWorkOrder, this.resourceType);
    },
    assignResource(resource) {
      this.currentResource = resource;
      this.assignQuantity = 1;
      this.assignTo = '';
      this.assignRemark = '';
      this.showAssignModal = true;
    },
    confirmAssignment() {
      if (!this.assignTo) {
        alert('请选择分配对象');
        return;
      }
      
      // 实际项目中这里会调用API提交分配信息
      console.log('分配资源', {
        resourceId: this.currentResource.id,
        quantity: this.assignQuantity,
        to: this.assignTo,
        remark: this.assignRemark,
        workOrderId: this.selectedWorkOrder
      });
      
      this.showAssignModal = false;
      this.$message.success('资源分配成功');
    }
  }
}
</script>

<style scoped>
.page-container {
  padding: 20px;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  margin-bottom: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.dispatch-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  padding: 20px;
  margin-top: 15px;
}

.filter-bar {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
  align-items: center;
}

.form-control {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  width: 200px;
}

.btn-search {
  background: #3498db;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.3s;
}

.btn-search:hover {
  background: #2980b9;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 10px;
}

.data-table th, .data-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

.data-table th {
  background-color: #f5f7fa;
  font-weight: 600;
}

.status-tag {
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  color: white;
}

.status-tag.available {
  background-color: #2ecc71;
}

.status-tag.maintenance {
  background-color: #f39c12;
}

.status-tag.low {
  background-color: #e74c3c;
}

.btn-assign {
  background: #3498db;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.3s;
}

.btn-assign:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 8px;
  width: 500px;
  max-width: 90%;
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #7f8c8d;
}

.modal-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.btn-cancel {
  padding: 8px 16px;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-confirm {
  padding: 8px 16px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn-confirm:hover {
  background: #2980b9;
}
</style>